package WSLinkProducts;

use strict;
use SOAP::Lite;
#+trace => 'debug';
use Log::Log4perl qw(:easy);
use Data::Dumper;

sub new
{
    my $class = $_[0];
    my $self;
    
    $self->{_productSearchRequest} = $_[1];
    $self->{_author} = $_[2];
    if ($_[3]) { SOAP::Lite->import(trace => 'debug'); }
    $self->{_soap} = SOAP::Lite
    											-> uri('')
										    	-> on_action( sub { join '/', 'http://XXXXXXXXXXX-mailorder.co.uk', 'ProductSearch'})
    											-> proxy('http://XXXXXXXXXXXks.co.uk/owsweb/owsweb.asmx');
    Log::Log4perl->init("WSImportProducts.log.config");
    $self->{_systemLogger} = Log::Log4perl->get_logger('XXXXXXXXXXX.products.import');
    bless $self, $class;
    return $self;
}

sub _setupParams {
			my $self = shift;
			my $page = shift;
			if (defined($page)) {
				#This is the place where we can override page.
			} else {
				$page = $self->{_productSearchRequest}->{Page};
			}
			my @params = (
				SOAP::Data->type('decimal')->name('PriceTo' => $self->{_productSearchRequest}->{PriceTo}),
			    SOAP::Data->type('decimal')->name('PriceFrom' => $self->{_productSearchRequest}->{PriceFrom}),
			    SOAP::Data->type('string')->name('SourceKey' => $self->{_productSearchRequest}->{SourceKey}),
			    SOAP::Data->type('string')->name('CustomerType' => $self->{_productSearchRequest}->{CustomerType}),
			    SOAP::Data->type('string')->name('CustomerNumber' => $self->{_productSearchRequest}->{CustomerNumber}),
			    SOAP::Data->type('string')->name('CurrencyCode' => $self->{_productSearchRequest}->{CurrencyCode}),
			    SOAP::Data->type('boolean')->name('InStock' => $self->{_productSearchRequest}->{InStock}),
			    SOAP::Data->type('boolean')->name('IncludeDiscounts' => $self->{_productSearchRequest}->{IncludeDiscounts}),
			    SOAP::Data->type('boolean')->name('IncludeFreeStock' => $self->{_productSearchRequest}->{IncludeFreeStock}),
			    SOAP::Data->type('boolean')->name('IncludePrices' => $self->{_productSearchRequest}->{IncludePrices}),
			    SOAP::Data->type('boolean')->name('IncludePurchases' => $self->{_productSearchRequest}->{IncludePurchases}),
			    SOAP::Data->type('boolean')->name('IncludeComponents' => $self->{_productSearchRequest}->{IncludeComponents}),
			    SOAP::Data->type('boolean')->name('IncludePromotions' => $self->{_productSearchRequest}->{IncludePromotions}),
			    SOAP::Data->type('boolean')->name('IncludeDocuments' => $self->{_productSearchRequest}->{IncludeDocuments}),
			    SOAP::Data->type('boolean')->name('IncludeCategories' => $self->{_productSearchRequest}->{IncludeCategories}),
			    SOAP::Data->type('boolean')->name('IncludeSalesOrderLines' => $self->{_productSearchRequest}->{IncludeSalesOrderLines}),
			    SOAP::Data->type('boolean')->name('IncludeSellingCodes' => $self->{_productSearchRequest}->{IncludeSellingCodes}),
			    SOAP::Data->type('string')->name('SortBy' => $self->{_productSearchRequest}->{SortBy}),
		        SOAP::Data->type('string')->name('SortOrder' => $self->{_productSearchRequest}->{SortOrder}),
			    SOAP::Data->type('int')->name('Page' => $page),
			    SOAP::Data->type('int')->name('RecordsPerPage' => $self->{_productSearchRequest}->{RecordsPerPage}),
			    SOAP::Data->type('string')->name('SearchValue' => $self->{_productSearchRequest}->{SearchValue}),
			    SOAP::Data->type('tns:SearchByType')->name('SearchBy' => $self->{_productSearchRequest}->{SearchBy}),
			    SOAP::Data->type('string')->name('Warehouse' => $self->{_productSearchRequest}->{Warehouse}),
			    SOAP::Data->type('string')->name('Category' => $self->{_productSearchRequest}->{Category})
			);
			$self->LogDebug("Web service request: \n" . Dumper(\@params));
			return @params;
}

sub _setupMethod {
				my $self = shift;
				my $method = SOAP::Data->name('ProductSearchRequest')
    												->attr({xmlns => 'http://XXXXXXXXXXX-mailorder.co.uk/'});
    		return $method;	
}

sub _setupHeaders {
				my $self = shift;
				my $Author = SOAP::Header->name('Author')
            ->attr({'xmlns' => 'http://XXXXXXXXXXX-mailorder.co.uk/'})
            ->value(\SOAP::Header->value(SOAP::Header->name('UserID' => $self->{_author}->{Username}),
                                         SOAP::Header->name('Password' => $self->{_author}->{Password})
				));
				my @headers = ($Author);
    		return @headers;	
}

sub ChangeRequestParameters {
				my $self = shift;
				my $NewPrices = shift;
				foreach my $cfg (keys %$NewPrices){
					$self->{_productSearchRequest}->{$cfg} = $NewPrices->{$cfg};
				}
}

sub LogDebug {
				my $self = shift;
				my $error_message = shift;
				$self->{_systemLogger}->debug($error_message);
}

sub LogInfo {
				my $self = shift;
				my $error_message =  shift;
				$self->{_systemLogger}->info($error_message);
}

sub LogWarn {
				my $self = shift;
				my $error_message =  shift;
				$self->{_systemLogger}->warn($error_message);
}

sub LogError {
				my $self = shift;
				my $error_message =  shift;
				$self->{_systemLogger}->error($error_message);
}

sub LogFatal {
				my $self = shift;
				my $error_message =  shift;
				$self->{_systemLogger}->fatal($error_message);
}

sub _hitMe {
				my $self = shift;
				my $page = shift;
				my $respond = $self->{_soap}->call($self->_setupMethod() => $self->_setupParams($page), $self->_setupHeaders())->result;
				if (ref($respond) eq "HASH") {
					return $respond->{'Product'};
			  } else {
			  	$self->LogError("No products to fetch.");	
				}
}

1;
